<?php
/**
 * 商品管理模型
 * @author e5
 *
 */
class GoodsModel extends Model{
	public $table = "goods";
	public $validate = array(
			array("cid","nonull","请选择分类",2,3),
			array("bid","nonull","请选择品牌",2,3),
			array("gname","nonull","请填写商品名称",2,3),
			array("gunit","nonull","请填写商品单位",2,3),
			array("gmarketprice","nonull","请填写商品市场价",2,3),
			array("gshopprice","nonull","请填写商品商城价",2,3),
			array("gpic","nonull","请选择列表图",2,3),
			array("piclist","nonull","请选择商品图册",2,3),
			array("intro","nonull","请填写商品详细信息",2,3),
			array("service","nonull","请填写售后服务",2,3)
	);
	public $auto = array(
			array("guptime","time","function",2,1),
			array("vshop_admin_aid","_getAid","method",2,1),
	);
	public $map = array(
			"cid"=>"vshop_category_cid",
			"bid"=>"vshop_brand_bid",
			"tid"=>"vshop_type_tid"
	);
	public function _getAid(){
		return session("aid");
	}
	/**
	 * 添加商品
	 */
	public function addGoods(){
		if (!$this->create()) return false;
		$gid = $this->add();
		if (!$gid){
			$this->error = "添加商品失败";
			return false;
		}
		//添加商品详细信息
		K("Detail")->addDetail($gid);
		//添加商品属性
		K("GoodsAttr")->addGoodsAttr($gid);
		return true;
	}
	/**
	 * 编辑商品
	 */
	public function editGoods() {
		if (!$this->create()) return false;
		$this->data['gallinventory'] = 0;
		$this->update();
		$gid = Q("post.gid",0,"intval");
		//更新商品详细信息表数据
		K("Detail")->editDetail($gid);
		//更新商品属性
		K("GoodsAttr")->editGoodsAttr($gid);
		return true;
	}
	/**
	 * 通过传入的分类id数组获得商品id
	 * @param array $cids
	 */
	public function cidsGetGids($cids){
		$gids = $this->where("vshop_category_cid IN (".implode(",", $cids).")")->getField('gid',true);
		return $gids;
	}
}